package com.yonyou.train.service;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import com.yonyou.train.dao.CourseDao;
import com.yonyou.train.dao.ReleaseCourseDao;
import com.yonyou.train.exception.CourseException;
import com.yonyou.train.exception.ReleaseCourseException;
import com.yonyou.train.model.Class;
import com.yonyou.train.model.Class_Sub;
import com.yonyou.train.model.User;
//import org.apache.catalina.servlet4preview.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class CourseService {
    @Autowired
    private CourseDao course;

  //有分页
  public List<Map> findAllCourse(int pagesize,int pageindex,int type, HttpServletRequest request) {
        if ("".equals(request.getHeader("token")) || null == request.getHeader("token")) {
            throw new CourseException("传入用户非法");
        }
        if (type < 0 || type >=4)
            throw new CourseException("传入课程状态类型非法");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd（E）HH:mm", Locale.CHINA);
        SimpleDateFormat df = new SimpleDateFormat("HH:mm");

        //分页效果
        int pageFirst=pageindex*pagesize;
         List<Map> data = course.findAllCourse( pageFirst,pagesize,type,Long.parseLong(request.getHeader("token")));
        List<Map> datainfo = new ArrayList<Map>();
        for (Map map : data) {
            Map m = new HashMap();
            m.put("class_id", map.get("id"));
            m.put("subject", map.get("subject"));
            m.put("time", sdf.format(map.get("starttime")).replaceAll("星期", "周") + "-" + df.format(map.get("endtime")));
            m.put("teacher", map.get("teacher_name"));
            m.put("assistant", map.get("assistant_name"));
            m.put("subclass_id", map.get("subclass_id"));
            m.put("address", map.get("address"));
            m.put("state", map.get("state"));
            datainfo.add(m);
        }
        return datainfo;
    }

}
